package com.demo.security.securitylogin;

import com.demo.security.pojo.User;
import com.demo.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component("myUserDetailService")
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      System.out.println("自定义用户认证："+username);

       User user =userService.login(username);
        if (user==null){
            throw  new UsernameNotFoundException("用户或密码错误");
        }
        System.out.println("自定义用户认证逻辑，用户是："+user);
        org.springframework.security.core.userdetails.User result= new org.springframework.security.core.userdetails.User(username,user.getPassword(),AuthorityUtils.createAuthorityList());
        return result;
    }
}
